<?php
/**
 * Created by PhpStorm.
 * User: zhengze
 * Date: 2019/7/1
 * Time: 18:29
 */

namespace app\api\service;

use app\api\model\Record;
use common\exception\RecordException;
use common\helper\DateHelper;
use common\service\BaseService;
use common\token\Token;
use common\traits\InstanceTrait;

;

/**
 * Class RecordService
 * @package app\api\service
 */
class RecordService extends BaseService
{
    use InstanceTrait;

    public function getRecordList($param)
    {
        $Record = Record::instance();
        $field = [
            "record_id",
            "content",
            "exe_time",
            'from_unixtime(exe_time)' => "exe_date",
            "type",
        ];
        $user = Token::instance()->getUserInfo();
        $param['creater_id'] = $user[$user->getPk()];
        $res = $Record->getRecordList($param, $field);
        return $res;
    }

    public function viewRecord($param)
    {
        $Record = Record::instance();
        $user = Token::instance()->getUserInfo();
        $param['creater_id'] = $user[$user->getPk()];
        $data = $Record->getRecordInfo($param);
        return $data;
    }

    public function save($post)
    {
        $data = $post;
        $member_info = Token::instance()->getUserInfo();
        $minute = DateHelper::to_date($data['exe_date'], "i");
        $minute = $minute >= 30 ? '30' : '00';
        $data['exe_date'] = DateHelper::to_date($data['exe_date'], "Y-n-j H:{$minute}:00");
        $data['exe_time'] = DateHelper::to_time($data['exe_date']);
        $where = [
            ['exe_time', '=', $data['exe_time']],
        ];
        if ($ret = Record::instance()->getOne($where)) {
            $list = Record::instance()->allowField(true)->save($data, $where);
        } else {
            $member_info = Token::instance()->getUserInfo();
            $data['member_id'] = $member_info[$member_info->getPk()];
            $list = Record::instance()->save($data);
        }
        return $ret;
    }

}